package com.glory.hikvisionmobile.activity;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.adapter.LotAddListAdapter;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.glory.hikvisionmobile.base.Constant;
import com.glory.hikvisionmobile.bean.LotSplitOrAdd.LotAddBean;
import com.glory.hikvisionmobile.bean.LotSplitOrAdd.LotSplitBean;
import com.glory.hikvisionmobile.bean.net.BaseResponseBean;
import com.glory.hikvisionmobile.utils.DialogUtils;
import com.glory.hikvisionmobile.utils.EditTextEnterUtils;
import com.glory.hikvisionmobile.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Lot_LotAddActivity extends BaseActivity {

    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvStepDetails)
    TextView tvStepDetails;
    @BindView(R.id.tvRecipe)
    TextView tvRecipe;
    @BindView(R.id.tvProductDetails)
    TextView tvProductDetails;
    @BindView(R.id.tvLotNumber)
    TextView tvLotNumber;
    @BindView(R.id.etLotNum)
    FreeUI_ClearEditText etLotNum;
    @BindView(R.id.rvLotAddList)
    RecyclerView rvLotAddList;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    @BindView(R.id.tvReplay)
    TextView tvReplay;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvRecipeDesc)
    TextView tvRecipeDesc;
    @BindView(R.id.tvProductName)
    TextView tvProductName;
    private LotAddListAdapter lotAddListAdapter;
    private LotAddListAdapter lotAddListAdapter1;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lot_add;
    }

    @Override
    protected void initView() {
        //待合批次列表
        rvLotAddList.setLayoutManager(new LinearLayoutManager(Lot_LotAddActivity.this));
        rvLotAddList.setItemAnimator(new DefaultItemAnimator());
        rvLotAddList.addItemDecoration(new DividerItemDecoration(Lot_LotAddActivity.this, 1));
        lotAddListAdapter = new LotAddListAdapter(null);
        lotAddListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        lotAddListAdapter.isFirstOnly(true);
        rvLotList.setAdapter(lotAddListAdapter);
        //扫描批次列表
        rvLotList.setLayoutManager(new LinearLayoutManager(Lot_LotAddActivity.this));
        rvLotList.setItemAnimator(new DefaultItemAnimator());
        rvLotList.addItemDecoration(new DividerItemDecoration(Lot_LotAddActivity.this, 1));
        lotAddListAdapter1 = new LotAddListAdapter(null);
        lotAddListAdapter1.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        lotAddListAdapter1.isFirstOnly(true);
        rvLotAddList.setAdapter(lotAddListAdapter1);
        initEdit(etLot, s -> requestData(s, "", ""));
        EditTextEnterUtils.initEnterToUpperCase(etLotNum);
        FreeApi_EditTextUtils.setEditTextListener(etLotNum, this, s -> searchLot(s));
    }

    private void searchLot(String lotID) {
        String contain = "N";
        List<LotAddBean> data = lotAddListAdapter1.getData();
        if (data.size() <= 0) {
            showConfirmDialog("未获取到列表数据");
            etLotNum.setText("");
            etLotNum.requestFocus();
            return;
        }

        for (LotAddBean bean : data) {
            if (bean.getLotId().equals(lotID)) {
                if (lotAddListAdapter.getData().contains(bean)) {
                    contain = "C";
                    break;
                }
                contain = "Y";
                lotAddListAdapter.addData(bean);
                lotAddListAdapter.notifyDataSetChanged();
                etLotNum.setText("");
                etLotNum.requestFocus();
                break;
            }
        }

        if (contain.equals("N")) {
            showToast("当前批次不在列表内", false);
            etLotNum.setText("");
            etLotNum.requestFocus();
        } else if (contain.equals("C")) {
            showToast("批次已添加", false);
            etLotNum.setText("");
            etLotNum.requestFocus();
        }


    }

    private void requestData(String lotID, String actionType, String mergeLotIdList) {
        JsonObject map = new JsonObject();
        map.addProperty("lotId", lotID);
        map.addProperty("actionType", actionType);
        map.addProperty("mergeLotIdList", mergeLotIdList);
        NetUtils.requestDara("HIK.PdaLotMerge", this, map, new FreeUI_DialogEntityCallBack<BaseResponseBean<LotSplitBean>>(
                new TypeToken<BaseResponseBean<LotSplitBean>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotSplitBean>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    if (actionType.equals("Merge")) {
                        showConfirmDialog(Lot_LotAddActivity.this, "合批成功");
                        return;
                    }
                    LotSplitBean lotSplitBean = response.body().getBody();
                    tvLotNumber.setText(lotSplitBean.getMainQty());
                    tvProductDetails.setText(lotSplitBean.getPartDesc());
                    tvRecipe.setText(lotSplitBean.getRecipeName());
                    tvStepDetails.setText(lotSplitBean.getStepDesc());
                    lotAddListAdapter1.setNewData(lotSplitBean.getxPdaLots());
                    lotAddListAdapter.setMergeLotID();
                    lotAddListAdapter.setNewData(new ArrayList<LotAddBean>());
                    tvProductName.setText(lotSplitBean.getPartName());
                    tvRecipeDesc.setText(lotSplitBean.getRecipeDesc());
                    etLotNum.requestFocus();

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotSplitBean>> response) {
                super.onError(response);
                loadError(response.getException(), "HIK.PdaLotMerge");

            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("批次合批");
        freeUI_titleView.setRightListener("刷新", v -> {
            initPermission("Refresh", this, new ActionControlListener() {
                @Override
                public void onControlChangeListener() {
                    if (editIsEmpty(etLot, "批次号不能为空")) {
                        requestData(etLot.getText().toString().trim(), "", "");
                    }
                }
            });

        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    @OnClick(R.id.tvReplay)
    public void onViewClicked() {
        initPermission("Submit", this, new ActionControlListener() {
            @Override
            public void onControlChangeListener() {
                if (lotAddListAdapter.getMergeLotID().length() <= 0) {
                    showToast("当前未选中待合批次", false);
                    return;
                }
                if (editIsEmpty(etLot, "批次号不能为空")) {
                    DialogUtils.showLoginDialog(Lot_LotAddActivity.this, Constant.CURRENT_USER, () ->
                            requestData(etLot.getText().toString().trim(), "Merge", lotAddListAdapter.getMergeLotID()), getSupportFragmentManager());


                }
            }
        });

    }

}
